package com.music.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

//���ع�����
public class DownloadFile {
	private DefaultHttpClient httpclient = new DefaultHttpClient();
	/**
	 * �����ı��ļ�
	 */
	// get
		public int getMethod(String url,Map<String, String> param,String directory,String fileName) {
			String htmlString = "";
			try {
				StringBuffer sb=new StringBuffer();
				
				String fileUrl=FileManager.getExternalStorage()+File.separator+directory+File.separator+fileName;
				boolean isExists=FileManager.isExists(fileUrl);
				if(isExists)
				{
					return 0;
				}
				
				if(param!=null)
				{
				for (Map.Entry<String, String> entry : param.entrySet()) {
					sb.append("&"+entry.getKey()+"="+  URLEncoder.encode(entry.getValue(),"UTF-8"));
				}
				}
				HttpUriRequest httpget = new HttpGet(url+sb.toString());
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				HttpResponse response = httpclient.execute(httpget);
				HttpEntity entity = response.getEntity();
				InputStream inputStream=entity.getContent();
				 System.out.println(">>>>>>>>>>"+EntityUtils.toString(entity));
				BufferedInputStream bis = new BufferedInputStream(inputStream);
				BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(fileUrl));
				   byte[] buffer=new byte[4*1024];
				   int temp;
				   while((temp=bis.read(buffer))!=-1){
					   bos.write(buffer,0,temp);
				   }
				   bos.close();
				   bis.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return 1;
		}
	/**
	 * �������ļ�
	 */
	public int downloadMedia(String urlStr,String directory,String fileName)
	{
		try
		{
			URL url=new URL(urlStr);
			HttpURLConnection connection=(HttpURLConnection)url.openConnection();
			BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
			FileManager.createDirectory(directory);
			String fileUrl=FileManager.getExternalStorage()+File.separator+directory+File.separator+fileName;
			boolean isExists=FileManager.isExists(fileUrl);
			if(isExists)
			{
				return 0;
			}
			
			BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(fileUrl));
			   byte[] buffer=new byte[4*1024];
			   int temp;
			   while((temp=bis.read(buffer))!=-1){
				   bos.write(buffer,0,temp);
			   }
			   bos.close();
			   bis.close();
			 
		} catch (IOException e) {
			   e.printStackTrace();
			   return -1;
		}	 
		return 1;
	}
}
